<link rel="stylesheet" type="text/css" href="<?= base_url() ?>file/js/easyui/themes/gray/easyui.css">
<link rel="stylesheet" type="text/css" href="<?= base_url() ?>file/js/easyui/themes/icon.css">
<?php if($user_group=='10'){ ?>
   
   <script>
function confSubmit(form) {
if (confirm("Apakah Anda Yakin Ingin Memindahkan Pengelola Partner?")) {
form.submit();
}
else {
alert("Pemindahan Pengelola Dibatalkan");
}
}
    function populateSourceRegion(){
      $('#sales_group1').removeAttr('checked');
       $('#sourceRegion').html("");
       $.ajax({
        
            url: "<?php echo base_url(); ?>index.php/partners/getRegion",
            dataType:"json",
            success:function(data){
               
                  $.each(data, function(i,n){
                        
                        $('#sourceRegion').append(new Option(n['territory_name'],n['territory_id']));
                    });
                    populateSourceCluster();//($('#source').val());
            },
            error: function(data){
            }
       });  
       
    }
    
    function populateSourceCluster(){
        $('#sourceCluster').html("");
       $.ajax({
        
            url: "<?php echo base_url(); ?>index.php/partners/getCluster/"+$('#sourceRegion').val(),
            dataType:"json",
            success:function(data){
             
                  $.each(data, function(i,n){
                        
                        $('#sourceCluster').append(new Option(n['territory_name'],n['user_id']));
                    });
                   
            },
            error: function(data){
            }
       });  
       
    }   
    
     function populateDestinationRegion(){
      
       $('#destinationRegion').html("");
       $.ajax({
        
            url: "<?php echo base_url(); ?>index.php/partners/getRegion",
            dataType:"json",
            success:function(data){
               
                  $.each(data, function(i,n){
                        
                        $('#destinationRegion').append(new Option(n['territory_name'],n['territory_id']));
                    });
                    populateDestinationCluster();//($('#source').val());
            },
            error: function(data){
            }
       });  
       
    }
    
    function populateDestinationCluster(){
        
        $('#destinationCluster').html("");
       $.ajax({
        
            url: "<?php echo base_url(); ?>index.php/partners/getCluster/"+$('#destinationRegion').val(),
            dataType:"json",
            success:function(data){
             
                  $.each(data, function(i,n){
                        
                        $('#destinationCluster').append(new Option(n['territory_name'],n['user_id']));
                    });
                   
            },
            error: function(data){
            }
       });  
       
    }   
    
   
    function populateSourceSales(sales_group){
      
       $('#source').html("");
       $.ajax({
        
            url: "<?php echo base_url(); ?>index.php/partners/getSalesFromCluster/"+$('#sourceCluster').val()+"/"+sales_group,
            dataType:"json",
            success:function(data){
                $('#source').append(new Option('--Unmaintained--','Unmaintained'));
                  $.each(data, function(i,n){
                        
                        $('#source').append(new Option(n['user_name'],n['user_id']));
                        //$('#destination').append(new Option(n['user_name'],n['user_id']));
                    });
                    populatePartners($('#source').val());
            },
            error: function(data){
            }
       });  
       
    }
    
    function populateDestinationSales(sales_group){
      
        $('#destination').html("");
       $.ajax({
        
            url: "<?php echo base_url(); ?>index.php/partners/getSalesFromCluster/"+$('#destinationCluster').val()+"/"+sales_group,
            dataType:"json",
            success:function(data){
                $('#destination').append(new Option('--Unmaintained--','Unmaintained'));
                  $.each(data, function(i,n){
                        
                        //$('#source').append(new Option(n['user_name'],n['user_id']));
                        $('#destination').append(new Option(n['user_name'],n['user_id']));
                    });
                   // populatePartners($('#source').val());
            },
            error: function(data){
            }
       });  
       
    }
    
     function populatePartners(sales_id){
          $(".wide").empty();
      $.ajax({
        
            url: "<?php echo base_url(); ?>index.php/partners/getPartnersFromCluster/"+$('#sourceCluster').val()+"/"+$('input:radio:checked').val()+"/"+sales_id,
            dataType:"json",
            success:function(data){
                  $.each(data, function(i,n){
                    add_tuple=  "<tr class='wide'>"+
                                    "<td><input type='checkbox' name='territory_id["+n['territory_id']+"]' /></td>"+
                                    "<td>"+n['partner_id']+"</td>"+
                                    "<td>"+n['partner_name']+"</td>"+
                                    "<td>"+n['partner_address']+"</td>"+
                                    "<td>"+n['partner_city']+"</td>"+
                                "</tr>";                    
                    $('#table-updateable').append(add_tuple);
                    });
            },
            error: function(data){
            }
       });         
    }   
    
   populateSourceRegion();
   populateDestinationRegion();
</script>

<div class="box">
        <!-- box / title -->
        <div class="title">
                <h5>Pemindahan Pengelola Partner</h5>
        </div>
        <!-- end box / title -->        
      <div>
        <div>
            <fieldset>     
                <input type="radio" name="sales_group" id="sales_group" value="8" onchange="$('#hidden').val('8');populateSourceSales($('#hidden').val());populateDestinationSales($('#hidden').val())"/> Smartfren Ambassador
                <input type="radio" name="sales_group" id="sales_group" value="9" onchange= "$('#hidden').val('9');populateSourceSales($('#hidden').val());populateDestinationSales($('#hidden').val())"/> Direct Sales
                <input type="hidden" id="hidden" name="hidden" />
              </fieldset> 
        </div>
          <div  style="float:left;width:50%; padding-left:2%; clear:both; padding-top: 10px;">   
              <label>Nama Regional : </label>  <select name="sourceRegion" id="sourceRegion" onchange="populateSourceCluster();$('#source').html('');populateSourceSales($('#hidden').val());"></select> 
              <label>Nama Cluster : </label>  <select name="sourceCluster" id="sourceCluster" onchange="$('#source').html('');populateSourceSales($('#hidden').val());" ></select> 
              
          </div>
          <div  style="float:left;padding-top: 10px;">   
              <label>Nama Regional : </label>  <select name="destinationRegion" id="destinationRegion" onchange="populateDestinationCluster();$('#destination').html('');populateDestinationSales($('#hidden').val());"></select> 
              <label>Nama Cluster : </label>  <select name="destinationCluster" id="destinationCluster" onchange="$('#destination').html('');populateDestinationSales($('#hidden').val());"  ></select> 
             
          </div>
          <form id="form-partners-migration" action="<?=$_SERVER['PHP_SELF']?>" method="post">
              <div style="float:left;width:50%; padding-left:2%; clear:both; padding-top: 10px;">
                Nama Sales Terdahulu: <select name="source" id="source" onchange="populatePartners($('#source').val());"></select>
              </div>
              <div style="float: left; padding-top: 10px;">
                Nama Sales Selanjutnya: <select name="destination" id="destination"></select>
              </div>
              <div style="clear: both; padding-top:15px;">       
                   <center><h2 style="color: green;"><?=$status;?></h2> </center>      
                            <table width="92%" style="margin-left: 19px;" id="table-updateable" class="table-right-panel">
                                <tbody>
                                    <tr align="center" id="tr_select_all" class="tr-colour3">
                                        <th><!--<input type="checkbox" onclick="check_all()" id="select_all" />--></th>
                                        <th>Kode Partner</th>
                                        <th>Nama Partner</th>
                                        <th>Alamat</th>    
                                        <th>Kota</th>              
                                    </tr>
                                </tbody>
                            </table>                      
              </div>
              <br />
              <br />
              <input type="button" onClick="confSubmit(this.form);" value="Pindahkan" style="margin-left: 2%;" />
              
          </form>
      </div>  
</div>
   
<?} else {?>
<script>
function confSubmit(form) {
if (confirm("Apakah Anda Yakin Ingin Memindahkan Pengelola Partner?")) {
form.submit();
}
else {
alert("Pemindahan Pengelola Dibatalkan");
}
}

    function populateSales(sales_group){
      
       $('#source').html("");
        $('#destination').html("");
       $.ajax({
        
            url: "<?php echo base_url(); ?>index.php/partners/getSales/"+sales_group,
            dataType:"json",
            success:function(data){
                $('#source').append(new Option('--Unmaintained--','Unmaintained'));
                
                $('#destination').append(new Option('--Unmaintained--','Unmaintained'));
                  $.each(data, function(i,n){
                        
                        $('#source').append(new Option(n['user_name'],n['user_id']));
                        $('#destination').append(new Option(n['user_name'],n['user_id']));
                    });
                    populatePartners($('#source').val());
            },
            error: function(data){
            }
       });  
       
    }
    
     function populatePartners(sales_id){
          $(".wide").empty();
      $.ajax({
        
            url: "<?php echo base_url(); ?>index.php/partners/getPartners/"+$('input:radio:checked').val()+"/"+sales_id,
            dataType:"json",
            success:function(data){
                  $.each(data, function(i,n){
                    add_tuple=  "<tr class='wide'>"+
                                    "<td><input type='checkbox' name='territory_id["+n['territory_id']+"]' /></td>"+
                                    "<td>"+n['partner_id']+"</td>"+
                                    "<td>"+n['partner_name']+"</td>"+
                                    "<td>"+n['partner_address']+"</td>"+
                                    "<td>"+n['partner_city']+"</td>"+
                                "</tr>";                    
                    $('#table-updateable').append(add_tuple);
                    });
            },
            error: function(data){
            }
       });         
    }   
</script>

<div class="box">
        <!-- box / title -->
        <div class="title">
                <h5>Pemindahan Pengelola Partner</h5>
        </div>
        <!-- end box / title -->        
      <div>
          <div>
            <h2><?php echo $cluster['member_display'];?></h2>
          </div>
          <div style="padding-left:2%;">   
              <fieldset>     
                <input type="radio" name="sales_group" id="sales_group" value="8" onchange="populateSales(8);"/> Outlet
                <input type="radio" name="sales_group" id="sales_group" value="9" onchange= "populateSales(9);"/> Agent
              </fieldset>    
          </div>
          <form id="form-partners-migration" action="<?=$_SERVER['PHP_SELF']?>" method="post">
              <div style="float:left;width:50%; padding-left:2%; clear:both; padding-top: 10px;">
                Nama Sales Terdahulu: <select name="source" id="source" onchange="populatePartners($('#source').val());"></select>
              </div>
              <div style="float: left; padding-top: 10px;">
                Nama Sales Selanjutnya: <select name="destination" id="destination"></select>
              </div>
              <div style="clear: both; padding-top:15px;">       
                   <center><h2 style="color: green;"><?=$status;?></h2> </center>      
                            <table width="92%" style="margin-left: 19px; font-size: 14px" id="table-updateable" class="table-right-panel">
                                <tbody>
                                    <tr align="center" id="tr_select_all" class="tr-colour3">
                                        <th><!--<input type="checkbox" onclick="check_all()" id="select_all" />--></th>
                                        <th>Kode Partner</th>
                                        <th>Nama Partner</th>
                                        <th>Alamat</th>    
                                        <th>Kota</th>              
                                    </tr>
                                </tbody>
                            </table>                      
              </div>
              <br />
              <br />
              <input type="button" onClick="confSubmit(this.form);" value="Pindahkan" style="margin-left: 2%;" />
              
          </form>
      </div>  
</div>
<? } ?>